package com.cam.test.elasticsearch.controller;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.Refresh;
import co.elastic.clients.elasticsearch.core.*;
import com.cam.test.elasticsearch.entity.ElasticsearchTestDM;
import com.cam.test.elasticsearch.entity.ElasticsearchUser;
import com.cam.test.elasticsearch.service.ElasticsearchTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

@RestController
@RequestMapping("/elasticsearch")
public class ElasticsearchTestController {

    @Autowired
    private ElasticsearchClient client;

    @Autowired
    private ElasticsearchTestService service;

    public String insertIndex(String indexName) {
        return "";
    }

    @GetMapping("/search/user/test")
    public String searchUserTest() {
        SearchRequest searchRequest = SearchRequest.of(s -> {
            return s.index("user_index").query(q -> {
                return q.match(m -> {
                    return m.field("name").query("Wang");
                });
            });
        });
        try {
            SearchResponse<ElasticsearchUser> response = client.search(searchRequest, ElasticsearchUser.class);
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    @GetMapping("/document/insert/file")
    public String insertFile() {
        String filePath = "C:\\Users\\Doposoft\\Desktop\\Elasticsearch.docx";
        try {
            byte[] bytes = Files.readAllBytes(Paths.get(filePath));
            String content = Base64.getEncoder().encodeToString(bytes);
            ElasticsearchTestDM testDM = new ElasticsearchTestDM();
            testDM.setFileContent(content);
            testDM.setFileName("测试Elasticsearch Word文件");
            IndexRequest<ElasticsearchTestDM> indexRequest = IndexRequest.of(b -> {
                b.pipeline("attachment");
                b.id("g7j6VIQBujd5zS-MBNng");
                return b.index("document_management").document(testDM);
            });
            IndexResponse indexResponse = client.index(indexRequest);
            System.out.println(indexResponse);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        return "";
    }

    @GetMapping("/document/delete")
    public String delete() {
        try {
            DeleteResponse response = client.delete(DeleteRequest.of(builder -> {
                return builder.index("document_management").id("DPe2UIQBERZbnlirMlJY");
            }));
            System.out.println("delete");
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        return "";
    }

}
